package com.tt.dp.observer.subject;

import com.tt.dp.observer.observer.Observer;

import java.util.ArrayList;
import java.util.List;

public class WeatherData implements Subject {

    private List<Observer> observerList;

    private float temp;

    private float humidity;

    private float pressure;

    public WeatherData() {
        this.observerList = new ArrayList<Observer>();
    }

    public void registerObserver(Observer observer) {
        this.observerList.add(observer);
    }

    public void removeObserver(Observer observer) {
        int index = this.observerList.indexOf(observer);

        if (index > 0) {
            this.observerList.remove(index);
        }
    }

    public void notifyObserver() {
        for (Observer observer : observerList) {
            observer.update(this.temp, this.humidity, this.pressure);
        }
    }

    public void measurementsChanged() {
        notifyObserver();
    }

    public void setMeasurements(float temp, float humidity, float pressure) {

        this.temp = temp;
        this.humidity = humidity;
        this.pressure = pressure;

        measurementsChanged();
    }
}
